layui.use(['layer','form','table','element'],function () {
    var layer = layui.layer
        ,form = layui.form
        ,table =layui.table
        ,element = layui.element;

    //初始化数据表格
     table.render({
         elem:"#data"
         ,height: 'full-150'
         ,url:"/admin/comment/getTableDate"
         ,cellMinWidth: 80
         ,cols: [[
             {field:'userName',width:150, title: '用户名'}
             ,{field:'email', width:150,title: '邮箱地址', align:'center'}
             ,{field:'url',  title: '网址'}
             ,{field:'content',  width: 250,title: '内容'}
             ,{field:'articleId',width: 100,title: '文章ID',templet:'<div><a target="_Blank" style="color: #1f79ec" href="https://www.daydayzhuan.com/article/{{ d.articleId }}">{{ d.id }}</a></div>'}
             ,{field:'showCreateTime',width:190,title: '评论时间',sort: true}
             ,{field:'',width:90,title: '状态',toolbar: '#statusBar'}
             ,{field:'',width:280,title: '操作',toolbar: '#barDemo'}
         ]]
         ,page:true
     });

     //监听工具条
     table.on('tool(dataTable)',function (obj) {
         var data = obj.data;
         if(obj.event == 'check_pass'){
             //审核通过
             layer.confirm('确定审核通过？', function(index){
                 layer.msg('执行中...', { icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '50%', time: 300000 });

                 $.ajax({
                     type:"POST"
                     ,url:"/admin/comment/opt"
                     ,data:{
                         id:data.id,
                         action:"pass"
                     }
                     ,success:function (result) {
                         layer.msg(result.message);
                         table.reload("data");
                     }
                     ,error:function () {
                         layer.msg("系统错误");
                     }

                 });

             });

         }else if(obj.event == 'del'){
            //删除
             layer.confirm('确定删除？', function(index){
                 layer.msg('执行中...', { icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '50%', time: 300000 });

                 $.ajax({
                    type:"POST"
                     ,url:"/admin/comment/opt"
                     ,data:{
                        id:data.id,
                         action:"del"
                     }
                     ,success:function (result) {
                         layer.msg(result.message);
                         table.reload("data");
                     }
                     ,error:function () {
                         layer.msg("系统错误");
                     }

                 });

             });
         }else if(obj.event == 'check_fail') {
             //审核失败
             layer.confirm('确认审核失败？', function (index) {
                 layer.msg('执行中...', {
                     icon: 16,
                     shade: [0.5, '#f5f5f5'],
                     scrollbar: false,
                     offset: '50%',
                     time: 300000
                 });

                 $.ajax({
                     type: "POST"
                     , url: "/admin/comment/opt"
                     , data: {
                         id: data.id,
                         action: "fail"
                     }
                     , success: function (result) {
                         layer.msg(result.message);
                         table.reload("data");
                     }
                     , error: function () {
                         layer.msg("系统错误");
                     }

                 });

             });
         }else if(obj.event == 'reply'){
             layer.confirm('<input type="text" placeholder="回复" id="memo" name="memo" class="layui-input">', function(index){
                 layer.msg('操作中...', { icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '50%', time: 300000 });

                 $.ajax({
                     type:"POST"
                     ,url:"/admin/comment/reply"
                     ,data:{
                         id:data.id,
                         content:$("#memo").val()
                     }
                     ,success:function (result) {
                         layer.msg(result.message);
                         table.reload("data");
                     }
                     ,error:function () {
                         layer.msg("系统错误");
                     }

                 });

             });
         }
     });

    function rightMenu(){

        $(".layui-tab-title li",window.parent.document).on("contextmenu",function (e) {
            var rightmenu = $(".rightmenu",window.parent.document);
            rightmenu.find("li").attr('data-id',$(this).attr("lay-id"));
            l = e.clientX;
            t = e.clientY;
            rightmenu.css({ left:l, top:t}).show();
            return false;
        });
        //左键点击隐藏
        $("body,.layui-tab-title li",window.parent.document).click(function(){
            $(".rightmenu").hide();
        });

    }

    function iframeWH(){
        var H = $(window).height();
        $("iframe",window.parent.document).css("height",H+"px");
    }
     
     //搜索
    $("#search").on("click",function () {
        table.reload("data",{
            page:{
                curr:1
            }
            ,where:{
                title:$("#title").val(),
            }
        })
    });;

    $("#add").on("click",function () {
        var url = "/admin/project/freeToAdd";
        $(this).attr("data-url",url);
        parent.addTab($(this));
    });


    $("#updateSiteMap").on("click",function () {

        $.ajax({
            type:"POST"
            ,url:"/admin/system/updateSiteMap"
            ,success:function (result) {
                layer.msg(result.message);
            }
            ,error:function () {
                layer.msg("系统错误");
            }

        });


    });

});
